package com.family.menage.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.family.menage.controller.vo.CityStateVo;
import com.family.menage.controller.vo.CitycountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.family.menage.entity.AddressEntity;
import com.family.menage.service.AddressService;
import com.family.common.utils.PageUtils;
import com.family.common.utils.R;



/**
 * 
 *
 * @author wen
 * @email 1023076426@qq.com
 * @date 2021-09-12 19:00:57
 */
@RestController
@RequestMapping("menage/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 按字母排序展示所有的城市
     * @return
     */
    @RequestMapping("/city")
    public R cityAll(@RequestParam(name = "firstword",defaultValue = "")String firstword){
        List<AddressEntity> addressEntities=addressService.cityAll(firstword);
        return R.ok().put("data",addressEntities);
    }

    /**
     * 查询城市的状态以及所包含的各个区
     */
    @RequestMapping("/cityState/{pid}")
    public R cityState(@PathVariable("pid")Integer pid){
        List<CityStateVo> cityStateVos=addressService.cityStateByPid(pid);
        return R.ok().put("data",cityStateVos);
    }

    /**
     * 各个省的禁用和启用城市
     */
    @RequestMapping("provinces")
    public R provinces(){
        List<CitycountVo> citycountVos=addressService.cityStateCount();
        return R.ok().put("data",citycountVos);
    }

    /**
     * 三级树
     * @return
     */
    @RequestMapping("/tree")
    public R tree(){
        List<AddressEntity> addressEntities=addressService.listWithtree();
        return R.ok().put("data",addressEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		AddressEntity address = addressService.getById(id);

        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address){
		addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		addressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
